package com.example.wuyurun

import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView

class StudentAdapter(private var students: MutableList<Student>) :
    RecyclerView.Adapter<StudentAdapter.StudentViewHolder>() {

    class StudentViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        private val idTextView: TextView = itemView.findViewById(R.id.tv_student_id)
        private val nameTextView: TextView = itemView.findViewById(R.id.tv_student_name)
        private val genderTextView: TextView = itemView.findViewById(R.id.tv_student_gender)
        private val classTextView: TextView = itemView.findViewById(R.id.tv_student_class)

        fun bind(student: Student) {
            idTextView.text = student.id
            nameTextView.text = student.name
            genderTextView.text = student.gender
            classTextView.text = student.className
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): StudentViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.student_item, parent, false)
        return StudentViewHolder(view)
    }

    override fun onBindViewHolder(holder: StudentViewHolder, position: Int) {
        holder.bind(students[position])
    }

    override fun getItemCount(): Int = students.size

    @SuppressLint("NotifyDataSetChanged")
    fun updateList(newList: MutableList<Student>) {
        students.clear()
        students.addAll(newList)
        notifyDataSetChanged()
    }
}